package callnumber.gtdev5.com.photoVault.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.gtdev5.geetolsdk.mylibrary.util.DensityUtils;

/**
 * Created by cheng
 * PackageName FakeCall
 * 2018/6/26 11:38
 */

@SuppressLint("AppCompatCustomView")
public class MeizuView extends ImageView {
    //设置wrap_content时候View的大小
    private int defaultWidth = 100;
    private int defaultHeight = 100;
    private int beginX,beginY;
    private int oneceTop;
    private int oneceBottom;
    private int oneceLeft;
    private int oneceRight;
    private boolean isMeasure=false;
    private boolean isOnceTouch=true;
    private boolean isFirstMove=true;
    private float firstX; //控件初始X坐标
    private float firstY;//控件初始Y坐标
    private float firstMovegetY; //第一次滑动的Y坐标；
    public MeizuViewScrolllitener meizuViewScrolllitener;
    public MeizuView(Context context) {
        super(context);
    }
    public MeizuView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public MeizuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x= (int) event.getX();
        int y= (int) event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                beginX=x;
                beginY=y;
                firstX=getX();
                firstY=getY();
                if (!isMeasure){
                    oneceTop=getTop();
                    oneceBottom=getBottom();
                    oneceLeft=getLeft();
                    oneceRight=getRight();
                }
                isMeasure=true;
                Log.e("TAG",getX()+"beginX"+getX()+"beginY");
                break;
            case MotionEvent.ACTION_MOVE :
                if (isFirstMove){
                    firstMovegetY=getY();
                    isFirstMove=false;
                }
                    //计算拖动距离
                    int moveX=x-beginX;
                    int moveY=y-beginY;
                    int px=  DensityUtils.Dp2Px(getContext(),70);
                 if (getY()>firstMovegetY-px && getY()<firstMovegetY+px) {  //设定左右滑动的边界值
                     layout(getLeft() + moveX, getTop() + moveY, getRight() + moveX, getBottom() + moveY);
                     if (getX() < firstX - px) {
                         if (isOnceTouch) {
                             meizuViewScrolllitener.onLeftScrollListener();
                             isOnceTouch = false;
                         }

                     }
                     if (getX() > firstX + px) {
                         if (isOnceTouch) {
                             meizuViewScrolllitener.onRightScrollListener();
                             isOnceTouch = false;
                         }
                     }

                     Log.e("TAG", "move_x" + getX() + "   move_y:" + getY());
                 }
                break;
            case MotionEvent.ACTION_UP:
                layout(oneceLeft,oneceTop,oneceRight,oneceBottom);
                break;
        }
        return true;
    }
    //简单的重写onMeasure
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(defaultWidth,defaultHeight);
        }else if(widthSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(defaultWidth,heightSpecSize);
        }else if(heightSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSpecSize, defaultHeight);
        }else{
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    }

    public void setMeizuViewScrolllitener(MeizuViewScrolllitener meizuViewScrolllitener){
        this.meizuViewScrolllitener=meizuViewScrolllitener;
    }

    public interface MeizuViewScrolllitener{
        void onLeftScrollListener();
        void onRightScrollListener();
    }
}
